package com.cwt_taxi_xa.protocol.socket;

import java.io.ByteArrayOutputStream;
//Component

/**
 * 组装905信息
 */
public class MessageAssembly {

    MessageCallback messageCallback;

    public MessageAssembly(MessageCallback messageCallback) {
        this.messageCallback = messageCallback;
    }

    /**
     * 7E进行粘包处理
     */
    public ByteArrayOutputStream baos = new ByteArrayOutputStream(1024 * 50);
    public int pic_data_index_center = 0;

    public synchronized void nb_center(byte[] data) {
        try {
            for (int i = 0; i < data.length; i++) {
                if (pic_data_index_center == 0 && data[i] != 0x7E) {
                    continue;
                }
                baos.write(data[i]);
                pic_data_index_center++;
                if (data[i] == 0x7E && pic_data_index_center > 1) {
                    if(this.messageCallback != null){
                        this.messageCallback.callback(baos.toByteArray());
                    }
                    baos.reset();
                    pic_data_index_center = 0;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public interface MessageCallback {
        void callback(byte[] data);
    }
}
